package cn.linran.pet.service.impl;

import cn.linran.pet.domain.PetType;
import cn.linran.pet.mapper.PetTypeMapper;
import cn.linran.pet.service.IPetTypeService;
import cn.linran.basic.service.impl.BaseServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * 业务实现类：
 */
@Service
public class PetTypeServiceImpl extends BaseServiceImpl<PetType> implements IPetTypeService {
    @Autowired
    private PetTypeMapper petTypeMapper;
    @Override
    public List<PetType> typeTree() {
        // 1. 查询所有
        List<PetType> all = petTypeMapper.loadAll();
        System.out.println("所有宠物类型集合："+all);
        //所有宠物类型集合：[PetType(name=狗, description=所有狗类型, dirPath=/1, pid=null, children=[])
        // , PetType(name=猫, description=所有猫类型, dirPath=/2, pid=null, children=[])
        // , PetType(name=宠物猫, description=宠物, dirPath=/2/3, pid=2, children=[])
        // , PetType(name=中国田园猫, description=家猫，捉老鼠, dirPath=/2/4, pid=2, children=[])
        // , PetType(name=波斯猫, description=宠物, dirPath=/2/3/5, pid=3, children=[])
        // , PetType(name=银渐层, description=宠物, dirPath=/2/3/6, pid=3, children=[])
        // , PetType(name=蓝猫, description=宠物, dirPath=/2/3/7, pid=3, children=[])
        // , PetType(name=英短, description=宠物, dirPath=/2/3/8, pid=3, children=[])
        // , PetType(name=布偶猫, description=宠物, dirPath=/2/3/9, pid=3, children=[])
        // , PetType(name=狸花猫, description=家猫，捉老鼠, dirPath=/2/4/10, pid=4, children=[])
        // , PetType(name=橘猫, description=家猫，捉老鼠, dirPath=/2/4/11, pid=4, children=[])
        // , PetType(name=宠物狗, description=宠物, dirPath=/1/12, pid=1, children=[])
        // , PetType(name=中华田园犬, description=可以看门，忠诚, dirPath=/1/13, pid=1, children=[])
        // , PetType(name=柯基, description=宠物, dirPath=/1/12/14, pid=12, children=[])
        // , PetType(name=泰迪, description=宠物, dirPath=/1/12/15, pid=12, children=[])
        // , PetType(name=秋田犬, description=可以看门，忠诚, dirPath=/1/13/16, pid=13, children=[])
        // , PetType(name=牧羊犬, description=边境放羊, dirPath=/1/13/17, pid=13, children=[])
        // , PetType(name=雪拉瑞, description=宠物, dirPath=/1/12/18, pid=12, children=[])
        // , PetType(name=金毛, description=宠物, dirPath=/1/12/19, pid=12, children=[])
        // , PetType(name=鸟, description=需要一个笼子, dirPath=/20, pid=null, children=[])
        // , PetType(name=鱼, description=水缸中养, dirPath=/21, pid=null, children=[])]


        // 2 .准备一个map
        HashMap<Long, PetType> map = new HashMap<>();

        // 3.把所有数据放入map中
        for (PetType petType : all) {
            map.put(petType.getId(),petType);
        }
        System.out.println("将宠物类型以pet_id:petType放入map："+map);
        //将宠物类型以pet_id:petType放入map：{1=PetType(name=狗, description=所有狗类型, dirPath=/1, pid=null, children=[])
        // , 2=PetType(name=猫, description=所有猫类型, dirPath=/2, pid=null, children=[])
        // , 3=PetType(name=宠物猫, description=宠物, dirPath=/2/3, pid=2, children=[])
        // , 4=PetType(name=中国田园猫, description=家猫，捉老鼠, dirPath=/2/4, pid=2, children=[])
        // , 5=PetType(name=波斯猫, description=宠物, dirPath=/2/3/5, pid=3, children=[]), 6=PetType(name=银渐层, description=宠物, dirPath=/2/3/6, pid=3, children=[]), 7=PetType(name=蓝猫, description=宠物, dirPath=/2/3/7, pid=3, children=[]), 8=PetType(name=英短, description=宠物, dirPath=/2/3/8, pid=3, children=[]), 9=PetType(name=布偶猫, description=宠物, dirPath=/2/3/9, pid=3, children=[]), 10=PetType(name=狸花猫, description=家猫，捉老鼠, dirPath=/2/4/10, pid=4, children=[]), 11=PetType(name=橘猫, description=家猫，捉老鼠, dirPath=/2/4/11, pid=4, children=[]), 12=PetType(name=宠物狗, description=宠物, dirPath=/1/12, pid=1, children=[]), 13=PetType(name=中华田园犬, description=可以看门，忠诚, dirPath=/1/13, pid=1, children=[]), 14=PetType(name=柯基, description=宠物, dirPath=/1/12/14, pid=12, children=[]), 15=PetType(name=泰迪, description=宠物, dirPath=/1/12/15, pid=12, children=[]), 16=PetType(name=秋田犬, description=可以看门，忠诚, dirPath=/1/13/16, pid=13, children=[]), 17=PetType(name=牧羊犬, description=边境放羊, dirPath=/1/13/17, pid=13, children=[]), 18=PetType(name=雪拉瑞, description=宠物, dirPath=/1/12/18, pid=12, children=[]), 19=PetType(name=金毛, description=宠物, dirPath=/1/12/19, pid=12, children=[]), 20=PetType(name=鸟, description=需要一个笼子, dirPath=/20, pid=null, children=[]), 21=PetType(name=鱼, description=水缸中养, dirPath=/21, pid=null, children=[])}

        // 4. 准备一个list，保存处理之后的数据
        ArrayList<PetType> typeTree = new ArrayList<>();
        //遍历所有宠物类型集合
        for (PetType petType : all) {
            if(petType.getPid()==null){ // 如果是顶级宠物类型，直接加入typeTree集合
                typeTree.add(petType);
            }else{ // 如果是子宠物类型，放入上级部门的children中
                // 获取parent_id
                Long pid = petType.getPid();
                // 获取上级部门对象
                PetType parentPetType = map.get(pid);

                // 把自己放入上级部门的children中
                parentPetType.getChildren().add(petType);
            }
        }
        System.out.println("宠物树:"+typeTree);
        //宠物树:[PetType(name=狗, description=所有狗类型, dirPath=/1, pid=null, children=[PetType(name=宠物狗, description=宠物, dirPath=/1/12, pid=1, children=[PetType(name=柯基, description=宠物, dirPath=/1/12/14, pid=12, children=[]), PetType(name=泰迪, description=宠物, dirPath=/1/12/15, pid=12, children=[]), PetType(name=雪拉瑞, description=宠物, dirPath=/1/12/18, pid=12, children=[]), PetType(name=金毛, description=宠物, dirPath=/1/12/19, pid=12, children=[])]), PetType(name=中华田园犬, description=可以看门，忠诚, dirPath=/1/13, pid=1, children=[PetType(name=秋田犬, description=可以看门，忠诚, dirPath=/1/13/16, pid=13, children=[]), PetType(name=牧羊犬, description=边境放羊, dirPath=/1/13/17, pid=13, children=[])])])
        // , PetType(name=猫, description=所有猫类型, dirPath=/2, pid=null, children=
        //                                                                          [PetType(name=宠物猫, description=宠物, dirPath=/2/3, pid=2, children=
        //                                                                                                                                              [PetType(name=波斯猫, description=宠物, dirPath=/2/3/5, pid=3, children=[]), PetType(name=银渐层, description=宠物, dirPath=/2/3/6, pid=3, children=[]), PetType(name=蓝猫, description=宠物, dirPath=/2/3/7, pid=3, children=[]), PetType(name=英短, description=宠物, dirPath=/2/3/8, pid=3, children=[]), PetType(name=布偶猫, description=宠物, dirPath=/2/3/9, pid=3, children=[])]), PetType(name=中国田园猫, description=家猫，捉老鼠, dirPath=/2/4, pid=2, children=[PetType(name=狸花猫, description=家猫，捉老鼠, dirPath=/2/4/10, pid=4, children=[]), PetType(name=橘猫, description=家猫，捉老鼠, dirPath=/2/4/11, pid=4, children=[])])])
        // , PetType(name=鸟, description=需要一个笼子, dirPath=/20, pid=null, children=[])
        // , PetType(name=鱼, description=水缸中养, dirPath=/21, pid=null, children=[])]

        // 返回部门树
        return typeTree;

    }
}
